<?php

namespace App\Models;

use App\Http\Resources\GoodsCategoryCollection;
use App\Http\Resources\PetCategoryCollection;

class PetCategory extends Base
{
    protected function add($inputs)
    {
        $this->fill($inputs);
        $this->save();
    }

    protected function index($inputs)
    {
        if (isset($inputs['name']) && !empty($inputs['name'])) {
            $data = self::query()
                ->orderBy('sequence', 'desc')->select([
                    'id',
                    'pid',
                    'name',
                    'sequence',
                ])->where('name', $inputs['name'])->get();
            $data = (new PetCategoryCollection($data))->response()->getData(true);
            return response()->json(['data' => $data['data']]);
        } else {
            $data = self::query()
                ->orderBy('sequence', 'desc')->select([
                    'id',
                    'pid',
                    'name',
                    'sequence',
                ])->get();
            $data = (new PetCategoryCollection($data))->response()->getData(true);
            return response()->json(['data' => make_tree($data['data'])]);
        }
    }

    protected function edit($inputs)
    {
        $this->where('id', $inputs['id'])->update($inputs);
    }

    protected function del($inputs)
    {
        $this->where('id', $inputs['id'])->delete();
        $this->where('pid', $inputs['id'])->delete();
    }
}
